<?php


if( !defined('FileData_class') )
{	
  define('FileData_class', 'true', false);
  
   
  class FileData extends databaseData
  {
	var $category;
	var $owner;
	var $created_date;
	var $description;
	var $comment;
	var $status;
	var $department;
	var $view_users;
	var $read_users;
	var $write_users;
	var $admin_users;
	var $filesize;
	var	$isLocked;
	
	function FileData($id, $connection, $database)
	{
		$this->field_name = 'realname';
		$this->field_id = 'id';
		$this->result_limit = 1;  
		$this->tablename = $this->TABLE_DATA;
		databaseData::databaseData($id, $connection, $database);
		
		$this->loadData();
	}
	function exists()
	{
	    $query = "SELECT * FROM {$GLOBALS['CONFIG']['db_prefix']}$this->tablename WHERE {$GLOBALS['CONFIG']['db_prefix']}$this->tablename.id = $this->id";
	    $result = mysql_query($query, $this->connection);
	    switch(mysql_num_rows($result))
	    {
	      case 1: return true; break;
	      case 0: return false; break;
	      default: $this->error = 'Non-unique'; return $this->error; break;
	    }
	}
	
	
	function loadData()
	{
		$query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->tablename.category,{$GLOBALS['CONFIG']['db_prefix']}$this->tablename.owner, 
			{$GLOBALS['CONFIG']['db_prefix']}$this->tablename.created, {$GLOBALS['CONFIG']['db_prefix']}$this->tablename.description, 
			{$GLOBALS['CONFIG']['db_prefix']}$this->tablename.comment, {$GLOBALS['CONFIG']['db_prefix']}$this->tablename.status, 
			{$GLOBALS['CONFIG']['db_prefix']}$this->tablename.department 
			FROM {$GLOBALS['CONFIG']['db_prefix']}$this->tablename WHERE {$GLOBALS['CONFIG']['db_prefix']}$this->tablename.id = $this->id";
		
		$result = mysql_query($query, $this->connection) or die ("Error in query: $query. " . mysql_error());
		if( mysql_num_rows($result) == $this->result_limit )
		{
			while( list($category, $owner, $created_date, $description, $comment, $status, $department) = mysql_fetch_row($result) )
			{
				$this->category = $category;
				$this->owner = $owner;
				$this->created_date = $created_date;
				$this->description = stripslashes($description);
				$this->comment = stripslashes($comment);
				$this->status = $status;
				$this->department = $department;
			}
		}
		else
			$this->error = 'Non unique file id';
		$this->isLocked = $this->status==-1;
	}

	function getFileSize()
	{	return $this->filesize;	}

	function getCategory()
	{	return $this->category;		}

	function getCategoryName()
	{	
		$query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_CATEGORY.name FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_CATEGORY WHERE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_CATEGORY.id = $this->category";
		$result = mysql_query($query, $this->connection) or die ("Error in query: $query. " . mysql_error());
		if( mysql_num_rows($result) == $this->result_limit)
			list($name) = mysql_fetch_row($result);
		else 
		{
				$this->error = 'Non unique file id';
				return $this->error;
		}
		return $name;
	}
	function isOwner($uid)
	{	return ($this->getOwner()==$uid);	}

	function getOwner()
	{	return $this->owner;		}
	
	function getOwnerName()
	{	
		$user_obj = new User($this->owner, $this->connection, $this->database);
		return $user_obj->getName();
	}

	function getOwnerFullName()
	{	
		$user_obj = new User($this->owner, $this->connection, $this->database);
		return $user_obj->getFullName();
	}
	
	
	function getOwnerDeptId()
	{
		$user_obj = new User($this->getOwner(), $this->connection, $this->database);
		return $user_obj->getDeptId();
	}
	
	
	function getOwnerDeptName()
	{
		$user_obj = new User($this->getOwner(), $this->connection, $this->database);
		return $user_obj->getDeptName();
	}
	
	
	function getDescription()
	{	return $this->description;	}

	function getComment()
	{	return $this->comment;		}

	function getUserIds($right)
	{
	  $result_array = array();
	  $owner_query = "SELECT owner FROM {$GLOBALS['CONFIG']['db_prefix']}$this->tablename WHERE id = $this->id";
	  $u_query = "SELECT uid FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS WHERE fid = $this->id and rights >= $right";

	  $non_prev_user_query = "SELECT uid FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS WHERE fid = $this->id AND rights < $right"; 
	  
	  $owner_result = mysql_query($owner_query, $this->connection) or die("Error in query: ".$owner_query . mysql_error() );
	  $u_result = mysql_query($u_query, $this->connection) or die("Error in query: " .$u_query . mysql_error() );

	  $non_prev_u_reslt = mysql_query($non_prev_user_query, $this->connection) or die("Error in query: " .$non_prev_user_query . mysql_error() );  
	  
	  $not_u_uid = array();
	  $d_uid = array();
	  for($i = 0; $i<mysql_num_rows($non_prev_u_reslt); $i++)
	  	list($not_u_uid[$i]) = mysql_fetch_row($non_prev_u_reslt);
	  
	  $d_query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER.id, {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.dept_id 
	  	FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS, {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER WHERE fid = $this->id AND 
	  	{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER.department = {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.dept_id and 
	  	{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.rights >= $right";
	  
	  for($i=0; $i<sizeof($not_u_uid); $i++)
	  {
		$d_query .= " and {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER.id != " . $not_u_uid[$i];
	  }
	  $d_result = mysql_query($d_query, $this->connection) or die("Error in query: " .$d_query . mysql_error() );	
	  if(sizeof($owner_result) != 1)
	  {
	    echo 'Error in DB, multiple ownership';
	    exit;
	  }
	  $owner_uid = mysql_fetch_row($owner_result);
	  for($i = 0; $i<mysql_num_rows($u_result); $i++)
	    list($u_uid[$i]) = mysql_fetch_row($u_result);
	  for($i = 0; $i<mysql_num_rows($d_result); $i++)
	    list($d_uid[$i]) = mysql_fetch_row($d_result);
	  
	  if( isset($owner_uid) && isset($u_uid) )
	  {	  $result_array = databaseData::combineArrays($owner_uid, $u_uid);	}
	  if( isset($result_array) && isset($d_uid) )
	  {	  $result_array = databaseData::combineArrays($result_array, $d_uid);	}
	  
	  mysql_free_result($owner_result);
	  mysql_free_result($u_result);
	  mysql_free_result($d_result);
	  return $result_array;
	}

	function getStatus()
	{	return $this->status;		}
	function setStatus($value)
	{	mysql_query("UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA set status=$value where id = $this->id") or die(mysql_error());}

	function getCheckerOBJ()
	{
		$user = new User($this->status, $this->connection, $this->database);
		return $user;
	}

	function getDepartment()
	{	return $this->department;	}

	function getDeptName()
	{
		$query ="SELECT name FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPARTMENT WHERE id = ".$this->getDepartment().';';
		$result = mysql_query($query, $this->connection) or die ("Error in query: $query. " . mysql_error());
        if (mysql_num_rows($result) == 0) {
            echo('ERROR: No database entry exists in department table for ID = '.$this->getDepartment().'.');
            return "ERROR";
            
        }
        if (mysql_num_rows($result) > 1) {
            echo('ERROR: Multiple database entries exist in department table for ID = '.$this->getDepartment().'.');
            return "ERROR";
            
        }

		list($dept) = mysql_fetch_row($result);
		return $dept;
	}

	function getCreatedDate()
	{	return $this->created_date;	}

	function getModifiedDate()
	{
		        
        $query = "SELECT modified_on FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_LOG WHERE id = '$this->id' ORDER BY modified_on DESC limit 1;";
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
        list($name) = mysql_fetch_row($result);       
        return $name;
	}

	function getRealName()
	{	return databaseData::getName();	}
	
	function getViewRightUserIds()
	{	return $this->getUserIds($this->VIEW_RIGHT);  }	  
	
	function getReadRightUserIds()
	{	return $this->getUserIds($this->READ_RIGHT);  }
	
	function getWriteRightUserIds()
	{	return $this->getUserIds($this->WRITE_RIGHT);  }
	
	function getAdminRightUserIds()
	{	return $this->getUserIds($this->ADMIN_RIGHT);  }
	
	function getNoneRightUserIds()
	{	return $this->getUserIds($this->NONE_RIGHT);  }

	function getForbiddenRightUserIds()
	{
	
	  $u_query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.uid FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS 
	  				WHERE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.fid = $this->id 
					AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.rights = $this->FORBIDDEN_RIGHT";
					
	  $d_query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER.id, {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.dept_id 
	  				FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS, {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER 
					WHERE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.fid = $this->id 
					AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER.department = {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.dept_id 
	  				AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS.rights = $this->FORBIDDEN_RIGHT";
	  
	  $u_result = mysql_query($u_query, $this->connection) or die("Error in query: " .$u_query . mysql_error() );
	  $d_result = mysql_query($d_query, $this->connection) or die("Error in query: " .$d_query . mysql_error() );
	  $d_uid = array();
	  $u_uid = array();
	  for($i = 0; $i<mysql_num_rows($u_result); $i++)
	    list($u_uid[$i]) = mysql_fetch_row($u_result);
	  for($i = 0; $i<mysql_num_rows($d_result); $i++)
	    list($d_uid[$i]) = mysql_fetch_row($d_result);

	  $result_array = databaseData::combineArrays(array(), $u_uid);
	  $result_array = databaseData::combineArrays($result_array, $d_uid);
	  
	  mysql_free_result($u_result);
	  mysql_free_result($d_result);
	  return $result_array;

	
	}

	function toUserOBJs($uid_array)
	{
	  $UserOBJ_array = array();
	  for($i = 0; $i<sizeof($uid_array); $i++)
	  { 
	    $UserOBJ_array[$i] = new User($uid_array[$i], $this->connection, $this->database);
	  }
	  return $UserOBJ_array;
	}

	function isPublishable()
	{
		$query = "SELECT publishable FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE id = '$this->id'";
		$result = mysql_query($query, $this->connection) or die('Error in query'. mysql_error());
		if(mysql_num_rows($result) != 1)
		{
			echo('DB error.  Unable to locate file id ' . $this->id . ' in table '.$GLOBALS['CONFIG']['db_prefix'].'data.  Please contact ' . $GLOBALS['CONFIG']['site_mail'] . 'for help');
			exit;
		}
		list($publishable) = mysql_fetch_row($result);
		mysql_free_result($result);
		return $publishable;
	}
	function isArchived()
	{
		$query = "SELECT publishable FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE id = '$this->id'";
		$result = mysql_query($query, $this->connection) or die('Error in query'. mysql_error());
		if(mysql_num_rows($result) != 1)
		{
			echo('DB error.  Unable to locate file id ' . $this->id . ' in table '.$GLOBALS['CONFIG']['db_prefix'].'data.  Please contact ' . $GLOBALS['CONFIG']['site_mail'] . 'for help');
			exit;
		}
		list($publishable) = mysql_fetch_row($result);
		mysql_free_result($result);
		return ($publishable == 2);
	}

	function Publishable($boolean = true)
	{
		$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA SET publishable ='$boolean', {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA.reviewer = '$this->id' WHERE id = '$this->id'";
		$result = mysql_query($query, $this->connection) or die("Error in query: $query" . mysql_error());
	}

	function getReviewerID()
	{
		$query = "SELECT reviewer FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE id = '$this->id'";
		$result = mysql_query($query, $this->connection) or die("Error in query: $query" . mysql_error());
		$num_hits = mysql_num_rows($result);
		if($num_hits != 1)
		{
			echo 'Multiple entry for same id(' . $this->id . ')';
			exit;
		}
		list($reviewer) = mysql_fetch_row($result);
		mysql_free_result($result);
		return $reviewer;
	}

	function getReviewerName()
	{
		$reviewer_id = $this->getReviewerID();
		if(isset($reviewer_id))
		{	
			$user_obj = new User($reviewer_id, $this->connection, $this->database);
			return $user_obj->getName();
		}
  	}

  	function getReviewerOBJ()
  	{
  		return (new User($this->getReviewerID(), $this->connection, $this->database));
  	}

	function setReviewerComments($comments)
	{
        $comments=addslashes($comments);
		$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA SET reviewer_comments='$comments' WHERE id='$this->id'";
		$result = mysql_query($query, $this->connection) or
		die("Error in query: $query" . mysql_error());
	}

	function getReviewerComments()
	{
		$query = "SELECT reviewer_comments FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE id='$this->id'";
		$result = mysql_query($query, $this->connection) or
			die("Error in query: $query" . mysql_error());
		if(mysql_num_rows($result) != 1)
		{
			echo('NON-UNIQUE entries in DB');
			exit;
		}
		list($comments) = mysql_fetch_row($result);
		mysql_free_result($result);
		return $comments;
	}
	function temp_delete()
	{
		$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA SET publishable = 2 WHERE id = $this->id";
		$result = mysql_query($query, $this->connection) or
			die("Error in query: $query" . mysql_error());
	}
	function undelete()
	{
		$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA SET publishable = 0 WHERE id = $this->id";
		$result = mysql_query($query, $this->connection) or die("Error in query: $query" . mysql_error());
	}
	function isLocked()
	{	return $this->isLocked;	}
  }
}
?>
